﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.SymptomHistory
{
    public class SurveyEdit : PatientBasePage
    {
        protected DateTime editRecordDate = DateTime.Now;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected List<DiseaseSymptomInfo> PrimarySymptomList;
        //protected List<DiseaseSymptomInfo> SecondSymptomList;
        protected List<SymptomHistoryInfo> SymHistoryList;
        protected int SurveyId;

        protected new void HandleCommon()
        {
            this.AddLocation("症状记录", "/SymptomHistory/Index.aspx");
            this.AddLocation("修改症状调查", "/SymptomHistory/SurveyEdit.aspx");
            this.SetTitle("修改症状调查");

            SurveyId = RequestTools.GetInt("id",0);
            if (SurveyId <= 0)
            {
                this.AddErrLine("无效的调查表");
                return;
            }

            SymHistoryList = PatientSymptomBLL.Instance.GetSymptomSurvey(PrData.Id, SurveyId);
            PrimarySymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PrData.DiseaseId, Constants.PRIMARY_SYMPTOM);
            /*SecondSymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PrData.DiseaseId, Constants.SECOND_SYMPTOM);*/

            if (SymHistoryList.Count > 0)
            {
                editRecordDate = SymHistoryList[0].RecordDate;
            }

        }

        protected new void HandleGet()
        {
           
        }

        protected new void HandlePost()
        {
            List<SymptomHistoryInfo> lst = ValidateForm();
            if (lst == null) return;

            bool succ = true;

            PatientSymptomBLL.Instance.UpdateSymptomSurvey(SurveyId, lst);
            if (succ)
            {
                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddMsgLine("修改失败");
            }
            this.SetUrl("/SymptomHistory/Index.aspx");
            this.SetMetaRefresh();
        }

        protected List<SymptomHistoryInfo> ValidateForm()
        {
     
            // 对时间进行判断
            string value = RequestTools.GetFormString("date");           

            if (value == "")
            {
                this.AddErrLine("请填写时间");
                return null;
            }
            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }

            DateTime now = DateTime.Today;
            List<SymptomHistoryInfo> lst = new List<SymptomHistoryInfo>();
            foreach (DiseaseSymptomInfo item in PrimarySymptomList)
            {
                SymptomHistoryInfo info = new SymptomHistoryInfo();
                info.SurveyId = SurveyId;
                info.PRId = PrData.Id;
                info.SymptomId = item.SymptomId;
                info.DegreeValue = RequestTools.GetFormInt("sym" + item.SymptomId, -1);
                info.Id = RequestTools.GetFormInt("sh" + item.SymptomId, -1);
                info.RecordDate = date;
                info.UpdateDate = now;
                lst.Add(info);
            }

            /*foreach (DiseaseSymptomInfo item in SecondSymptomList)
            {
                SymptomHistoryInfo info = new SymptomHistoryInfo();
                info.SurveyId = SurveyId;
                info.PRId = PrData.Id;
                info.SymptomId = item.SymptomId;
                info.DegreeValue = RequestTools.GetFormInt("sym" + item.SymptomId, -1);
                info.Id = RequestTools.GetFormInt("sh" + item.SymptomId, -1);
                info.RecordDate = date;
                info.UpdateDate = now;
                lst.Add(info);
            }*/

            return lst;
        }

        protected int GetSymptomHistoryData(int symid)
        {
            foreach (SymptomHistoryInfo info in SymHistoryList)
            {
                if (info.SymptomId == symid) return info.DegreeValue;
            }
            return Constants.SYMPTOM_NONE;
        }

        protected int GetSymptomHistoryId(int symid)
        {
            foreach (SymptomHistoryInfo info in SymHistoryList)
            {
                if (info.SymptomId == symid)
                {
                    return info.Id;
                }
            }
            return -1;
        }
    }
}